﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using DomenModel;

namespace Infrastructure.Repositories
{
    public class BaseInitializer : DropCreateDatabaseIfModelChanges<GeneralContext>
    {
        protected override void Seed(GeneralContext context)
        {
            // Role initializer
            var roles = new List<Role> 
            { 
                new Role { Name = "Administrator", Description = "It's admin" }, 
                new Role { Name = "Supervisor", Description = "It's super" }, 
                new Role { Name = "Merchandiser", Description = "It's merchick" }, 
                new Role { Name = "Customer", Description = "It's client" }
            };
            roles.ForEach(r => context.Roles.Add(r));
            context.SaveChanges();

            // Status initializer
            var statuses = new List<Status> 
            { 
                new Status { Title = "Standart", Value = 0, Discount = 0 }, 
                new Status { Title = "Silver", Value = 1000, Discount = 2 },
                new Status { Title = "Gold", Value = 3000, Discount = 5 },
                new Status { Title = "Platinum", Value = 10000, Discount = 10 },

            };
            statuses.ForEach(s => context.Statuses.Add(s));
            context.SaveChanges();

            // User initializer
            UserRepo userRepo = new UserRepo();
            var users = new List<User>();

            users.Add(new User { LoginName = "hash", FirstName = "hash", LastName = "hash", Password = "bhO5HanSkGzBnSXk4XxBpw==", Salt = "M8QhfMtdSh478Lv1DftwISUlxXjrSf9OfykFHNoQbp8=", Email = "hash@ha.sh", Region = "North", RoleId = 1, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "admin", FirstName = "admin", LastName = "admin", Password = "8LlB6DIEQVOdnJOi4NeYhw==", Salt = "wJduRUA+8PVUFV94GsIIpRsH6WdX9mguX1vJTVn6cdw=", Email = "admin@admin.ru", Region = "North", RoleId = 1, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "super", FirstName = "super", LastName = "super", Password = "nDdNDP/JJqY4d2dxsjJYJg==", Salt = "i7IJcbzZTbedm4f5BXv32RyuB40neAZvd4Szpjf+WYY=", Email = "super@super.ru", Region = "South", RoleId = 2, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "merch", FirstName = "merch", LastName = "merch", Password = "rRrw2iMuTpAOFyWRCw+C2w==", Salt = "fhyajYXi2+wIyES7f3X3X0kn3PBuSuWjzYauLGeARqM=", Email = "merch@merch.ru", Region = "West", RoleId = 3, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "cust", FirstName = "cust", LastName = "cust", Password = "xMU96eU300w8h2sGwc6/8A==", Salt = "3CTXU6W0kDVLqMPwUZDs/LA0DV8CZoElLsVGYwEP4/0=", Email = "cust@cust.ru", Region = "East", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "test", FirstName = "admin", LastName = "admin", Password = "gI6VUAmg0V0uO9+m7yLLQg==", Salt = "72+ALlU2bxAMcVtTOEOYDB8BfKj7CK1UtrE+9YemWxc=", Email = "admin@admin.ru", Region = "North", RoleId = 1, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "Naile", FirstName = "Naile", LastName = "Naile", Password = "Z1NhzPHFl3BQj84zHtWK8w==", Salt = "cIh7gNtX0UjRjGesfeNe3TkR80lSHiTAH/LndHVNiX4=", Email = "naile@admin.ru", Region = "South", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "Djamila", FirstName = "Djamila", LastName = "Aiams", Password = "kVp+APn6TLK/Kc3FGuBs9Q==", Salt = "XOqS3Mf7GSrOKdwlhad2gBE6xH4sPLIKHmakn+/4DMU=", Email = "djamila@admin.ru", Region = "West", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "Aerose", FirstName = "Rose", LastName = "Erose", Password = "L4oCdSmjCd5mUAtgfo2UDQ==", Salt = "BP5hhTIYudg/7Ln/5yJNrPCjzYZ3cHzL+UnDHFg/UZc=", Email = "rose@admin.ru", Region = "South", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "wildkill", FirstName = "Volkis", LastName = "Volkis", Password = "wyZ/hutbWDxjhXg5Amlv6g==", Salt = "eJghO2ojXme2UKMrk53zTa/0/XnUqH55WPOMj8tNWQs=", Email = "volkis@admin.ru", Region = "South", RoleId = 3, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "Rolshtein", FirstName = "Lshtein", LastName = "Rolshtein", Password = "vcbuHLs/y3uP5hkhuzNp4Q==", Salt = "/xowOA0yOaMkInH0O4QQH/fiipvhBS1E6i00dRk4bS0=", Email = "Rolshtein@admin.ru", Region = "West", RoleId = 3, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "Sarton", FirstName = "Sarton", LastName = "Sarton", Password = "wenMuHNp6KHzfyXEjpgqQA==", Salt = "2mhP7pd+OkfIznxgu+ouCPR06xBPA5MKBfnD98chKSY=", Email = "sartonn@admin.ru", Region = "West", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "merch1", FirstName = "Sarton", LastName = "Sarton", Password = "6ZByFIijA0lrSF2JtvY1MA==", Salt = "uU1SzYeY3G68yAnOtQl0+2TZ7qdXVoX8OsoefkCRMoE=", Email = "sartonn@admin.ru", Region = "West", RoleId = 4, StatusId = 3, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "merch2", FirstName = "Sarton", LastName = "Sarton", Password = "y5LPu1YxFphJj7UljkP0vA==", Salt = "WGvHGI2kEYdAPqVAY8tlNLgRNAL/tn90TxrNOzUJZ9U=", Email = "sartonn@admin.ru", Region = "West", RoleId = 4, StatusId = 3, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "merch3", FirstName = "Sarton", LastName = "Sarton", Password = "h7EhYlKFjNNJwxto53ZgTg==", Salt = "RTcs9oLk12p8QRUmtWQu/qrton/hkesQLlfj79yYIL0=", Email = "sartonn@admin.ru", Region = "West", RoleId = 4, StatusId = 3, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "merch4", FirstName = "Sarton", LastName = "Sarton", Password = "F1ThVn4UjejQiAjwr9ZkjA==", Salt = "c5g6YbZIAfCsPiN8THIaTMl0Y0RfcvDkgZtPuhYesxU=", Email = "sartonn@admin.ru", Region = "West", RoleId = 4, StatusId = 3, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "cust1", FirstName = "cust", LastName = "cust", Password = "b4AFs2CjuUpWglZMM/jegA==", Salt = "DGNwLROLUMp3roWi9khvPHb6smxUd7clJXkdWBXIhPw=", Email = "cust@cust.ru", Region = "East", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "cust2", FirstName = "cust", LastName = "cust", Password = "tD/BbbQuOCnH/dwgI3IZKg==", Salt = "BXiYgNF0gBMh4yrUHh+6/zq1kGYIIaDtAcjJ0gMxJRU=", Email = "cust@cust.ru", Region = "East", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "cust3", FirstName = "cust", LastName = "cust", Password = "b+EYJBpE9RkMtg8VfAG+Tw==", Salt = "hHmEfZHNcG9Cj0Ikzj/iyqjo8u7BRUBD26f9MB+MvOQ=", Email = "cust@cust.ru", Region = "East", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.Add(new User { LoginName = "cust3", FirstName = "cust", LastName = "cust", Password = "ezNRyqxUc/11ir2v7ZQDZw==", Salt = "7PRQXfjctBGFfa6qjMNCvS6g7mKh5c4a7YOGR5PVs9k=", Email = "cust@cust.ru", Region = "East", RoleId = 4, StatusId = 1, Balance = 0, IsActive = true });

            users.ForEach(u => context.Users.Add(u));
            context.SaveChanges();

            // Dimension initializer
            //var dimentions = new List<Dimension> 
            //{ 
            //    new Dimension { Name = "Item", Description="Unit of Product"}, 
            //    new Dimension { Name = "Box", Description="Container of several Items"}, 
            //    new Dimension { Name = "Package", Description="Pack of several Items"}
            //};
            //dimentions.ForEach(d => context.Dimensions.Add(d));
            //context.SaveChanges();

            // Item initializer
            var items = new List<Item> 
            {
                new Item { Name = "Nokia 1280", Description = "The best phone", ItemPrice = 234 }, 
                new Item { Name = "Nokia Asha 206 Black Dual", Description = "Русский текст", ItemPrice = 123 }, 
                new Item { Name = "Nokia C2-06 Touch and Type Golden ", Description = "Random Description", ItemPrice = 231 }, 
                new Item { Name = "Nokia X2-02 Dark Silver", Description = "The sun is shining", ItemPrice = 22 }, 
                new Item { Name = "Nokia X1-01 Dark Gray Dual", Description = "The weather is good", ItemPrice = 33 }, 
                new Item { Name = "Смартфон Nokia Lumia 720 Black", Description = "Two cats", ItemPrice = 232 }, 
                new Item { Name = "Samsung GT-E1195 Deep", Description = "Seven dogs", ItemPrice = 322 }, 
                new Item { Name = "Sony C1605 Xperia E Dual Champagne", Description = "Happy birthday to you", ItemPrice = 221 }, 
                new Item { Name = "Sony ST21i Xperia Tipo Black", Description = "Millions And Billions And Trillions Of Cats", ItemPrice = 222 }, 
                new Item { Name = "Смартфон Sony C1605 Xperia E Dual Black", Description = "Cranberries", ItemPrice = 2 },
                new Item { Name = "Apple", Description = "Huge, red, yammy apple", ItemPrice = 3 },
                new Item { Name = "Pine Apple", Description = "Just brought from Africa, you will definetely like it!", ItemPrice = 111},
                new Item { Name = "Bannana", Description = "Just brought from Africa! Somalia Pirates gathered them for you!", ItemPrice = 1},
                new Item { Name = "Strawberry", Description = "We Bet, you've never tasted so delicious one!", ItemPrice = 12},
                new Item { Name = "Cucumber", Description = "This is not only a vegetable for eating, you can also do a lot of handy things with it!! ", ItemPrice = 2345},
                new Item { Name = "Tomato", Description = "The great, mexican tomatoes with the unique taste", ItemPrice = 65},
                new Item { Name = "Onion", Description = "You will not cry while cutting it!", ItemPrice = 56},
                new Item { Name = "Eggplant", Description = "It's sooo huge!", ItemPrice = 456},
                new Item { Name = "herbage", Description = " an unnormal assortment of herbage helps you to make a delightful dish", ItemPrice = 34},
                new Item { Name = "Cocaine", Description = "You will never be boring", ItemPrice = 34},
                new Item { Name = "Marihuana", Description = "Leave all fears behind your back! Have a childish smile forever!", ItemPrice = 9},
                new Item { Name = "Green Tea", Description = "Just drink it once and you will never try anything else!", ItemPrice = 345},
            };
            items.ForEach(i => context.Items.Add(i));
            context.SaveChanges();

            // ItemDimension initializer
            var itemDimensions = new List<ItemDimension>();
            Random random = new Random();
            foreach (var i in items)
            {
                //foreach (var d in dimentions)
                //{
                itemDimensions.Add(new ItemDimension { Item = i, Dimension = 1, Price = (decimal)random.NextDouble() * 100, Quantity = random.Next(10) });
                //}
            }
            itemDimensions.ForEach(id => context.ItemDimensions.Add(id));
            context.SaveChanges();

            // CreditCard initializer
            var creditCards = new List<CreditCard> 
            {
                new CreditCard { CardType = "Visa", CVV2Code = 136, IssueNumber = 16343, CardNumber = 4619347895154365, ExpiryDate = DateTime.Today, StartDate = DateTime.Today },
                new CreditCard { CardType = "MasterCard", CVV2Code = 255, IssueNumber = 16112, CardNumber = 1919467654132164, ExpiryDate = DateTime.Today, StartDate = DateTime.Today },
                new CreditCard { CardType = "AmericanExpress", CVV2Code = 195, IssueNumber = 16163, CardNumber = 9787449965556111, ExpiryDate = DateTime.Today, StartDate = DateTime.Today },
                new CreditCard { CardType = "Maestro", CVV2Code = 132, IssueNumber = 16516, CardNumber = 1222344456667888, ExpiryDate = DateTime.Today, StartDate = DateTime.Today }
            };
            creditCards.ForEach(cc => context.CreditCards.Add(cc));
            context.SaveChanges();

            // Order initializer



            var orders = new List<Order>
            {
                new Order { Assigned = "merch1", CreditCardId = 1, CreditCard = creditCards.Find(cc => cc.CreditCardId == 1), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 4, user = users.Find(u => u.UserId == 4) },
                new Order { Assigned = "merch2", CreditCardId = 1, CreditCard = creditCards.Find(cc => cc.CreditCardId == 1), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 16, user = users.Find(u => u.UserId == 16) },
                new Order { Assigned = "merch3", CreditCardId = 1, CreditCard = creditCards.Find(cc => cc.CreditCardId == 1), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 4, user = users.Find(u => u.UserId == 4) },
                new Order { Assigned = "merch1", CreditCardId = 2, CreditCard = creditCards.Find(cc => cc.CreditCardId == 2), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 17, user = users.Find(u => u.UserId == 17) },
                new Order { Assigned = "merch2", CreditCardId = 2, CreditCard = creditCards.Find(cc => cc.CreditCardId == 2), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 18, user = users.Find(u => u.UserId == 18) },
                new Order { Assigned = "merch3", CreditCardId = 2, CreditCard = creditCards.Find(cc => cc.CreditCardId == 2), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 17, user = users.Find(u => u.UserId == 17) },
                new Order { Assigned = "merch1", CreditCardId = 3, CreditCard = creditCards.Find(cc => cc.CreditCardId == 3), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 16, user = users.Find(u => u.UserId == 16) },
                new Order { Assigned = "merch2", CreditCardId = 3, CreditCard = creditCards.Find(cc => cc.CreditCardId == 3), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 4, user = users.Find(u => u.UserId == 4) },
                new Order { Assigned = "merch3", CreditCardId = 3, CreditCard = creditCards.Find(cc => cc.CreditCardId == 3), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 18, user = users.Find(u => u.UserId == 18) },
                new Order { Assigned = "merch1", CreditCardId = 4, CreditCard = creditCards.Find(cc => cc.CreditCardId == 4), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 4, user = users.Find(u => u.UserId == 4) },
                new Order { Assigned = "merch2", CreditCardId = 4, CreditCard = creditCards.Find(cc => cc.CreditCardId == 4), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 18, user = users.Find(u => u.UserId == 18) },
                new Order { Assigned = "merch3", CreditCardId = 4, CreditCard = creditCards.Find(cc => cc.CreditCardId == 4), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 16, user = users.Find(u => u.UserId == 16) },
                new Order { Assigned = "merch4", CreditCardId = 4, CreditCard = creditCards.Find(cc => cc.CreditCardId == 4), DateOfOrdering = DateTime.Today, DeliveryDate = DateTime.Today, PreferibleDeliveryDate = DateTime.Today, UserId = 16, user = users.Find(u => u.UserId == 16) }

            
            };
            orders.ForEach(o => context.Orders.Add(o));
            context.SaveChanges();

            // OrderDetails initializer
            var orderDetails = new List<OrderDetails>();
            OrderDetails orderDetail;
            foreach (var o in orders)
            {

                for (int i = 0; i < 10; i++)
                {
                    float _count = random.Next(100);
                    int oId = random.Next(10);
                    decimal _price = (decimal)(random.NextDouble() * 10 * _count);
                    orderDetail = new OrderDetails { ItemDimensionId = i + 1, ItemDimension = itemDimensions.Find(id => id.ItemDimensionId == i + 1), Count = _count, Price = _price, OrderId = o.OrderId, Order = o };
                    orderDetails.Add(orderDetail);
                }

            }
            orderDetails.ForEach(od => context.OrderDetailses.Add(od));
            context.SaveChanges();

        }
    }
}
